<?php


namespace app\mingteng\controller;
use think\AjaxPage;
use think\Db;
use think\Cache;
use think\Env;



class Voucherqrcode extends Base
{

    public $listRows = 20;
    public function index(){
        return $this->fetch();
    }

    public function ajax_index(){
        $params = input('post.');
        $page = input('page', '1');

        $where = array();
        if (isset($params['keywords']) && !empty($params['keywords'])) {
            $where['v.voucherName'] = array('like', '%'.$params['keywords'].'%');
        }
        if($params['type']){
            $where['type'] = $params['type'];
        }
        $count = Db::name('voucher_qrcode')->alias('a')
            ->join('voucher v','v.voucherId =a.voucherId','LEFT')
            ->where($where)
            ->count();
        $list = Db::name('voucher_qrcode')->alias('a')
            ->join('voucher v','v.voucherId =a.voucherId','LEFT')
            ->where($where)
            ->order('a.id','desc')
            ->field('a.*,v.voucherName,v.voucherAmount')
            ->page($page, $this->listRows)
            ->select();
        if($list){
            foreach($list as $k=>$v){
                $list[$k]['addTime'] = date('Y-m-d H:i',$v['addTime']);
            }
        }
        $this->assign('list', $list);

        $ajaxPage = new AjaxPage($count, $this->listRows);
        $show = $ajaxPage->show();
        $this->assign('page', $show);

        return $this->fetch();
    }

    public function add(){

        $voucher = Db::name('voucher')->where('voucherType',2)->select();
        $this->assign('voucher', $voucher);
        return $this->fetch();
    }

    public function doAdd(){
        $params = input('post.');

        if (empty($params['voucherId'])) {
            return json(array('code' => 500, 'msg' => '请选择抵扣券'));
        }

        if (empty($params['number'])) {
            return json(array('code' => 500, 'msg' => '请输入生成数量'));
        }


        $catch = [];
        for($i=1;$i<= $params['number'];$i++){
            $encrData = $this->generateRandomString();
            $catch[$i]['voucherId'] = $params['voucherId'];
            $catch[$i]['qr_code'] = $this->generate_save($encrData);
            $catch[$i]['encrData'] = $encrData;
            $catch[$i]['addTime'] = time();
        }
        $couponId = Db::name('voucher_qrcode')->insertAll($catch);
        if($couponId){
            return json(array('code' => 200, 'msg' => '添加成功'));
        }else{
            return json(array('code' => 500, 'msg' => '添加失败'));
        }

    }

    public function del(){
        $del_id = input('del_id');

        Db::name('voucher_qrcode')->where('id', '=', $del_id)->delete();
        return json(array('code' => 200, 'msg' => '操作成功'));
    }

    /**
     * 生成随机数
     * @param int $length
     * @return string
     */
    public  function generateRandomString($length = 18) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $string = '';
        for ($i = 0; $i < $length; $i++) {
            $string .= $characters[rand(0, strlen($characters) - 1)];
        }
        return uniqid() . $string;
    }


    /**
     * 生成二维码
     * @return false|string
     */
    public function generate_save($encrData){

        vendor("phpqrcode.phpqrcode"); //引入库类
        $errorCorrectionLevel = 'L'; //容错级别
        $matrixPointSize = 10; //生成图片大小
        //生成二维码图片
        //判断是否有这个文件夹,没有的话就创建1个
        if(!is_dir("upload/file")){
            //创建文件夹
            mkdir("upload/file");
        }
        //设置二维码文件名
        $filename = 'upload/file/'.time().rand(10000,9999999).'.png';
        //生成二维码
        \PHPQRCode\QRcode::png($encrData, $filename, $errorCorrectionLevel, $matrixPointSize, 2);

        return '/'.$filename;

    }

}